<template>
  <div>
    <!-- checkList:选中元素的id -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="item in list"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}</el-checkbox>
      <!-- <el-checkbox label="复选框 A" />
      <el-checkbox label="复选框 C" /> -->
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSumbit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings.js'
import { getEmployeeById, updateRole } from '@/api/employee'
export default {
  props: { id: { type: String, required: true }},
  data() {
    return {
      checkList: [],
      list: []
    }
  },
  created() {
    this.loadRoles()

    // // 获取当前用户（id）已有角色
    // this.loadRolesById()
  },
  methods: {
    async loadRolesById() {
      const res = await getEmployeeById(this.id)
      console.log('当前用户（id）已有角色', res)
      // res.data.roleIds// ['640a78888b']
      // 数据回填
      this.checkList = res.data.roleIds
    },
    async loadRoles() {
      const res = await getRoles()
      console.log('全部的角色是', res)
      this.list = res.data.rows
    },
    closeDialog() {
      // 通知父组件关闭弹层
      this.$emit('close')
    },
    // 保存修改
    async hSumbit() {
      const res = await updateRole({ id: this.id, roleIds: this.checkList })
      console.log('保存修改', res)

      this.$message.success('保存成功')
      this.$emit('on-success')
    }
  }
}
</script>
